package communicationMessages;

import java.util.Map.Entry;
import java.util.logging.Level;

import logger.LoggerWriter;

import data.Forum;
import data.PostMessage;
import data.SubForum;
import data.ThreadMessage;

public class ViewThreadMessage extends Message {

	@Override
	public String getAnswer() {
		String ans = "postList\n";
		LoggerWriter logger  = LoggerWriter.getLoggerWriter();
		String arr[] = this.content.split("\n");
		if (arr.length != 2)
		{
			logger.log("wrong format ViewSubForumMessage",Level.WARNING);
			ans = "wrong format ViewSubForumMessage\n";
		}
		else
		{
			SubForum tSubForum = Forum.getForum().getSubForum(Integer.valueOf(arr[0]));
			if (tSubForum == null)
			{
				System.out.println("subforum is null");
				System.out.println("the number is " + arr[0] + "\n" + Integer.valueOf(arr[0]));
				return ans;
			}
			ThreadMessage tThraedMsgMessage = tSubForum.getThreadMessage(Integer.valueOf(arr[1]));
			ans+=tThraedMsgMessage.get_id() + ":" + tThraedMsgMessage.get_title() + "\n" + tThraedMsgMessage.get_content() + "\n";
			for (Entry<Integer, PostMessage> tEntry : tThraedMsgMessage.getResponse().entrySet()) {
				PostMessage tPostMsg = tEntry.getValue();
				ans+=tPostMsg.get_id() + ":" + tPostMsg.get_title() + "\n" + tPostMsg.get_content() + "\n";
				
			}
		}
		return ans;
	}

}
